package com.virtualtrainer_01.service;

import java.util.Random;

import android.content.Context;
import android.text.format.Time;

import com.virtualtrainer_01.dao.ClientDB;
import com.virtualtrainer_01.dao.ExerciseDB;
import com.virtualtrainer_01.dao.FoodDB;
import com.virtualtrainer_01.dao.GroupDB;
import com.virtualtrainer_01.model.Client;
import com.virtualtrainer_01.model.Exercise;
import com.virtualtrainer_01.model.Food;
import com.virtualtrainer_01.model.Group;
import com.virtualtrainer_01.model.Training;
import com.virtualtrainer_01.model.TrainingDay;
import com.virtualtrainer_01.util.NameList;

public class GenerateClientTraining {

	private Client client;
	private NameList<Training> trainingList;
	private NameList<TrainingDay> trainingDayList;
	private NameList<Food> foodList;
	private Context context;

	public GenerateClientTraining(Context context) {
		this.context = context;
		client = new Client();
		ClientDB clientDB = new ClientDB(context);
		clientDB.open();
		client = clientDB.getClient();
		clientDB.close();
		trainingList = new NameList<Training>();
		trainingDayList = new NameList<TrainingDay>();
		foodList = new NameList<Food>();
	}

	public Client getClient() {
		return client;
	}

	public void Generate() {
		int dispDays = client.getDisponibility();
		switch (dispDays) {
		case 2:
			for (int day = 0; day < dispDays; day++) {
				if (day == 0) {
					NameList<Exercise> re1 = new NameList<Exercise>();
					NameList<Group> rg1 = new NameList<Group>();
					NameList<Exercise> re2 = new NameList<Exercise>();

					// NameList<Exercise> re3 = new NameList<Exercise>();
					// NameList<Group> rg3 = new NameList<Group>();

					ExerciseDB edb = new ExerciseDB(context);
					edb.open();
					GroupDB gdb = new GroupDB(context);
					gdb.open();

					re1 = edb.getExerciseList("Piept");
					int limit = 4;
					re1.sortExercise(client, limit);

					re2 = edb.getExerciseList("Brat", "Biceps");
					re2.sortExercise(client, limit);

					for (int i = 0; i < re2.size(); i++) {
						re1.add(re2.get(i));
					}
					rg1.add(gdb.getGroup("Piept"));
					rg1.add(gdb.getGroup("Brat"));

					Time time = new Time(Time.getCurrentTimezone());
					time.setToNow();
					Random randomGenerator = new Random();
					int randomInt = randomGenerator.nextInt(10000);
					trainingList.add(new Training("Tpiept+biceps" + randomInt,
							rg1, 120, time, re1));

					edb.close();
					gdb.close();

					FoodDB fdb = new FoodDB(context);
					fdb.open();
					foodList.add((Food) fdb.getFood(1)); // doar de test
					fdb.close();

					trainingDayList.add(new TrainingDay((Training) trainingList
							.get(day), (Food) foodList.get(0), (Food) foodList
							.get(0), (Food) foodList.get(0)));
				} else {
					NameList<Exercise> re1 = new NameList<Exercise>();
					NameList<Group> rg1 = new NameList<Group>();
					NameList<Exercise> re2 = new NameList<Exercise>();
					// NameList<Exercise> re3 = new NameList<Exercise>();
					// NameList<Group> rg3 = new NameList<Group>();

					ExerciseDB edb = new ExerciseDB(context);
					edb.open();
					GroupDB gdb = new GroupDB(context);
					gdb.open();

					int limit = 4;
					re1 = edb.getExerciseList("Brat", "Triceps");
					re1.sortExercise(client, limit);

					re2 = edb.getExerciseList("Abdomen");
					re2.sortExercise(client, limit);
					rg1.add(gdb.getGroup("Brat"));
					rg1.add(gdb.getGroup("Abdomen"));
					for (int i = 0; i < re2.size(); i++) {
						re1.add(re2.get(i));
					}
					Time time = new Time(Time.getCurrentTimezone());
					time.setToNow();
					Random randomGenerator = new Random();
					int randomInt = randomGenerator.nextInt(10000);
					trainingList.add(new Training("Ttriceps" + randomInt, rg1,
							120, time, re1));

					edb.close();
					gdb.close();
					trainingDayList.add(new TrainingDay((Training) trainingList
							.get(day), (Food) foodList.get(0), (Food) foodList
							.get(0), (Food) foodList.get(0)));
				}
			}
			break;
		case 3:
			for (int day = 0; day < dispDays; day++) {
				if (day == 0) {
					NameList<Exercise> re1 = new NameList<Exercise>();
					NameList<Group> rg1 = new NameList<Group>();
					NameList<Exercise> re2 = new NameList<Exercise>();
					NameList<Exercise> re3 = new NameList<Exercise>();
					NameList<Group> rg3 = new NameList<Group>();

					ExerciseDB edb = new ExerciseDB(context);
					edb.open();
					GroupDB gdb = new GroupDB(context);
					gdb.open();

					re1 = edb.getExerciseList("Piept");
					int limit = 4;
					re1.sortExercise(client, limit);

					re2 = edb.getExerciseList("Brat", "Biceps");
					re2.sortExercise(client, limit);
					
					re3 = edb.getExerciseList("Abdomen");
					re3.sortExercise(client, limit);

					for (int i = 0; i < re2.size(); i++) {
						re1.add(re2.get(i));
					}
					for (int i = 0; i < re3.size(); i++) {
						re1.add(re3.get(i));
					}
					
					rg1.add(gdb.getGroup("Piept"));
					rg1.add(gdb.getGroup("Brat"));
					rg1.add(gdb.getGroup("Abdomen"));

					Time time = new Time(Time.getCurrentTimezone());
					time.setToNow();
					Random randomGenerator = new Random();
					int randomInt = randomGenerator.nextInt(10000);
					trainingList.add(new Training("Tpiept+biceps+abs" + randomInt,
							rg1, 120, time, re1));

					edb.close();
					gdb.close();

					FoodDB fdb = new FoodDB(context);
					fdb.open();
					foodList.add((Food) fdb.getFood(1)); // doar de test
					fdb.close();

					trainingDayList.add(new TrainingDay((Training) trainingList
							.get(day), (Food) foodList.get(0), (Food) foodList
							.get(0), (Food) foodList.get(0)));
				} else if(day == 1) {
					NameList<Exercise> re1 = new NameList<Exercise>();
					NameList<Group> rg1 = new NameList<Group>();
					NameList<Exercise> re2 = new NameList<Exercise>();
					NameList<Exercise> re3 = new NameList<Exercise>();					

					ExerciseDB edb = new ExerciseDB(context);
					edb.open();
					GroupDB gdb = new GroupDB(context);
					gdb.open();

					int limit = 4;
					re1 = edb.getExerciseList("Brat", "Triceps");
					re1.sortExercise(client, limit);
					re2 = edb.getExerciseList("Abdomen");
					re2.sortExercise(client, limit);
					re3 = edb.getExerciseList("Spate");
					re3.sortExercise(client, limit);
					
					rg1.add(gdb.getGroup("Brat"));
					rg1.add(gdb.getGroup("Spate"));
					rg1.add(gdb.getGroup("Abdomen"));
					for (int i = 0; i < re3.size(); i++) {
						re1.add(re3.get(i));
					}
					for (int i = 0; i < re2.size(); i++) {
						re1.add(re2.get(i));
					}					
					
					Time time = new Time(Time.getCurrentTimezone());
					time.setToNow();
					Random randomGenerator = new Random();
					int randomInt = randomGenerator.nextInt(10000);
					trainingList.add(new Training("Ttriceps+Spate+Abdomen" + randomInt, rg1,
							120, time, re1));

					edb.close();
					gdb.close();
					trainingDayList.add(new TrainingDay((Training) trainingList
							.get(day), (Food) foodList.get(0), (Food) foodList
							.get(0), (Food) foodList.get(0)));
				}
				
				else 
				{
					NameList<Exercise> re1 = new NameList<Exercise>();
					NameList<Group> rg1 = new NameList<Group>();
					NameList<Exercise> re2 = new NameList<Exercise>();
					NameList<Exercise> re3 = new NameList<Exercise>();
					NameList<Exercise> re4 = new NameList<Exercise>();

					ExerciseDB edb = new ExerciseDB(context);
					edb.open();
					GroupDB gdb = new GroupDB(context);
					gdb.open();

					int limit = 4;
					re1 = edb.getExerciseList("Brat", "Antebrat");
					re1.sortExercise(client, limit);
					re2 = edb.getExerciseList("Abdomen");
					re2.sortExercise(client, limit);
					re3 = edb.getExerciseList("Umeri");
					re3.sortExercise(client, limit);
					re4 = edb.getExerciseList("Picioare");
					re4.sortExercise(client, limit);
					
					rg1.add(gdb.getGroup("Brat"));
					rg1.add(gdb.getGroup("Umeri"));
					rg1.add(gdb.getGroup("Picioare"));
					rg1.add(gdb.getGroup("Abdomen"));
					for (int i = 0; i < re3.size(); i++) {
						re1.add(re3.get(i));
					}
					for (int i = 0; i < re4.size(); i++) {
						re1.add(re4.get(i));
					}
					for (int i = 0; i < re2.size(); i++) {
						re1.add(re2.get(i));
					}					
					
					Time time = new Time(Time.getCurrentTimezone());
					time.setToNow();
					Random randomGenerator = new Random();
					int randomInt = randomGenerator.nextInt(10000);
					trainingList.add(new Training("TAntebrat+Picioare+Umeri+Picioare" + randomInt, rg1,
							120, time, re1));

					edb.close();
					gdb.close();
					trainingDayList.add(new TrainingDay((Training) trainingList
							.get(day), (Food) foodList.get(0), (Food) foodList
							.get(0), (Food) foodList.get(0)));
				}	
			}
			break;
		default:
			break;
		}
	}

	public NameList<TrainingDay> getTrainingDayList() {
		return trainingDayList;
	}
}
